package com.atguigu.srb.core.controller.api;


import com.atguigu.srb.common.exception.Assert;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.common.result.Result;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.pojo.vo.UserBindVo;
import com.atguigu.srb.core.service.UserBindService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 前端控制器
 * </p>
 *
 * @author lsyandwh
 * @since 2022-12-02
 */
@Api(tags = "用户信息绑定")
@RestController
@RequestMapping("/api/core/userBind")
public class ApiUserBindController {

    @Autowired
    private UserBindService userBindService;

    @ApiOperation("用户绑定表单信息提交")
    @PostMapping("/commitBind")
    public Result commitBind(@RequestBody UserBindVo userBindVo, HttpServletRequest request){

        String userId = request.getHeader("userId");
        String from = userBindService.commitBind(userBindVo,userId);

        return Result.ok().data("from",from);
    }

    @ApiOperation("汇付宝绑定用户回调接口")
    @PostMapping("/notify")
    public String notifyBind(HttpServletRequest request){

        //获取汇付宝回调获取的所有参数
        Map<String, Object> map = RequestHelper.switchMap(request.getParameterMap());
        //签名校验
        boolean signEquals = RequestHelper.isSignEquals(map);
        Assert.isTrue(signEquals, ResponseEnum.WEIXIN_CALLBACK_PARAM_ERROR);

        userBindService.notifyBind(map);
        return "success";
    }

}

